/**
 * WeeShop 声明
 * ===========================================================
 * 网站： https://www.darlang.com
 * 标题： ECShop 小程序「weeshop 」- 基于 ECShop 为后台系统开发的非官方微信商城小程序
 * 链接： https://www.darlang.com/?p=709
 * 说明： 源码已开源并遵循 Apache 2.0 协议，你有权利进行任何修改，但请保留出处，请不要删除该注释。
 * ==========================================================
 * Copyright 2019 darlang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ===========================================================
 */

// list.js
import {PNT,setNavBarTitle,showToast,pushPagePath,scrollLoadList} from "../../../../utils/utils";
import { ReturnList, CancelReturnOrder} from "../../../../utils/apis";
// import {CheckUserLevel} from "../../../utils/publics";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    status: 0,
    tabStatus: [{id:1,label:'售后申请'},{id: 2,label:'进度查询'}],
    orderStatus: ['待付款','待发货','待收货','已收货','已完成','已取消'],
    orderLst: [],
    pages: {
      page: 1,
      size: 10,
      total: 10,
      done: false,
      loading: false
    },
    page:0,
    order_num:['order','pay','deliver'],
    flag:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opt) {
    setNavBarTitle(PNT.order.main);
    this.loginModal = this.selectComponent("#login-modal");
    var info = wx.getStorageSync('userInfo')
    this.setData({
      status: opt.status || 1,
      info:info
    });
    this.getOrderList();
    // this.getCancelReason();

  },

  /**
   * 页面跳转
   * @author darlang
   */
  pushPath(e) {
    const items = e.currentTarget.dataset;
    const pathData = [
      {type: 'order',path: '/pages/member/order/detail/detail?id=' + items.id},
      // {type: 'goods',path: '/pages/goods/detail/detail?id=' + items.id},
      {type: 'comment',path: '/pages/member/comment/add/add?orderId=' + items.id},
      {type: 'payment',path: '/pages/shopping/payment/payment?orderId=' + items.id},
      { type: 'shouhou', path: '/pages/member/order/return/return?orderId=' + items.id },
      { type: 'returndetail', path: '/pages/member/order/returnlist/detail?orderId=' + items.id },
      // {type: 'shipping',path: '/pages/member/order/shipping/index?id'},
    ];
    pushPagePath(e,pathData);
  },

  /**
   * 登录回调
   * @author darlang
   */
  loginCallback(cb) {
    if (cb.detail.type === 'success') {
      this.bindSorderTap();
    }
  },

  /**
   * 订单筛选
   *  0待付款 1待发货 2发货中 3已收货 4已评价 5已取消
   * @author darlang
   */
  bindSorderTap(e) {
    let id = e ? e.currentTarget.dataset.id : '0';
    let oid = this.data.status;
    if (e && id === oid) {
      return false;
    }
    this.setData({
      orderLst: [],
      page:0,
      status: id,
      flag:true,
    });
    this.getOrderList();
  },

  /**
   * 获取订单列表
   * @author darlang
   */
  getOrderList() {
    if(!this.data.flag){
      showToast('数据请求中');
      return;
    }
    var that = this;
    if (this.data.pages.done) {
      return false;
    }
    wx.showLoading({title: '加载中...',mask: true});
    var data = {};
    data.type = this.data.status;
    data.user_id = this.data.info.user_id;
    data.page = this.data.page;
    ReturnList(data).then(res => {
        if(res.error_code == 10000){
          var list = that.data.orderLst;
          for(var i=0;i<res.data.length;i++){
            list.push(res.data[i]);
          }
          this.setData({
            orderLst: list,
            flag:true,
          })
          
        }else{
          this.setData({
            flag: false,
          })
        }
        wx.hideLoading();

    });
  },



  /**
   * 取消订单
   * @author darlang
   * @param  {Number}   id 订单 id
   * @param  {Number}   reason 原因 id
   */
  CancelOrder(e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var that = this;
    wx.showModal({
      title: '温馨提示',
      content: '是否要取消当前售后记录？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({title: '正在查询...',mask: true});
          var data = {};
          data.ret_id = id;
          data.user_id = that.data.info.user_id;
          CancelReturnOrder(data).then(res => {
            if(res.error_code == 10000){
              showToast('订单已取消', 'success');
              var list = this.data.orderLst;
              list.splice(index,1);
              this.setData({
                orderLst:list,
              })
            }else{
              showToast(res.msg, 'warning');
              return;
            }
            // this.findOrderIndex(id,'cancel');
          }).catch(err => {
            // if (err.error_code === 10001) {
            //   showToast('重复取消','warning');
            //   this.findOrderIndex(id,'cancel');
            // }
          });
        }
      }
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.bindSorderTap();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.flag) {
      wx.showLoading({ title: '加载中...', mask: true });
      setTimeout(()=>{
        wx.hideLoading();
      },2000)
      return;
    }
    var page = this.data.page;
    page = page + 1;
    this.setData({
      page:page,
      flag:true,
    })
    this.getOrderList();
  }
});